﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace CommModule
{
    internal class XmlSerializationUtils
    {
        private static readonly String _defaultPackage = "CommModule.Messages.";

        internal static void GetConversionObjectXml(Object obj, XmlWriter xmlWriter)
        {
            MethodInfo method = obj.GetType().GetMethod("ConvertToCanonic");
            method.Invoke(obj, new object[] { xmlWriter });
        }

        internal static Object GetConversionXmlObject(XmlTextReader xmlTextReader)
        {
            String objectClass = xmlTextReader.Name;

            Type type = Type.GetType(_defaultPackage + objectClass);
            MethodInfo method = type.GetMethod("ConvertFromCanonic");

            Object obj = Activator.CreateInstance(type);
            method.Invoke(obj, new object[] { xmlTextReader });

            return obj;
        }
    }
}
